#include "mydb.h"
#include "mlog.h"

#include "sqlite/CppSQLite3.h"

char g_szDbFile[260] = {0};
CMyDb::CMyDb(void)
{
}

CMyDb::~CMyDb(void)
{
}

#define DB_FILE "db"
void CMyDb::Init()
{
	strncpy(g_szDbFile, DB_FILE, strlen(DB_FILE));
    printf("init database: %s\n", g_szDbFile);
	CppSQLite3DB db;
	try
	{
		db.open(g_szDbFile);
		if (!db.tableExists("user"))
		{
            printf("table user not exist, create it now!\n");
			db.execDML("create table user(id int, name char(20), pwd char(32), host char(20), port char(6), fd int);");
		}

        if (!db.tableExists("userlist"))
        {
            CMLog::Plog("Create userlist table.");
            db.execDML("create table userlist(userlid int, friendid int, groupid int, level int);");
        }

        if (!db.tableExists("userinfo"))
        {
            CMLog::Plog("Create userinfo table.");
            char sql[65536] = {0};
            sprintf(sql, "create table userinfo(userid int, name char(20), pwd char(32), host char(20), port int");
            sprintf(sql, "%s, sex int, both char(80), address char(2048), phone char(20), mobile char(15)", sql);
            sprintf(sql, "%s, school char(1024), email char(1024), homepage char(1024), job char(1024)", sql);
            sprintf(sql, "%s, post char(8), reserved1 char(1024), reserved2 char(1024));", sql);  
            CMLog::Plog("%d, %s", strlen(sql), sql);
            db.execDML(sql);
        }
	}
	catch (CppSQLite3Exception& e)
	{
		fprintf(stderr, "MYDB::Init SQLite3Exception %d : %s\n",e.errorCode(), e.errorMessage());
	//	cerr << e.errorCode() << ":" << e.errorMessage() << endl;
    	db.close();
	}
}


int CMyDb::Add(int nfd, char *host, int port, char *name, char *pwd)
{
    int id = GetUserId(name);
    if (id != 0)
    {
        CMLog::Plog("this user already existed, %s:%d", name, id);
        return 0;
    }

    int ret = 0;
	CppSQLite3DB db;
	try
	{
		db.open(g_szDbFile);

		CppSQLite3Buffer sql;
		sql.format("INSERT INTO user (id, fd, name, pwd, host, port) VALUES((select COUNT(*) from user)+1, %d, '%s', '%s', '%s', %d);", nfd, name, pwd, host, port);
        ret = db.execDML(sql);

        db.close();
	}
	catch (CppSQLite3Exception& e)
	{
        CMLog::Plog("CMyDb::Add: %d : %s", e.errorCode(), e.errorMessage());
        db.close();
	}

	return ret;
}

int CMyDb::GetUserId(char *name)
{
    int id = 0;
	CppSQLite3DB db;
	try
	{
		db.open(g_szDbFile);

		CppSQLite3Buffer sql;
        sql.format("SELECT id FROM user WHERE NAME='%s';", name);
		CppSQLite3Query q = db.execQuery(sql);
        while (!q.eof())
        {
            id = q.getIntField(0);
            q.nextRow();
            if (!q.eof())
            {
                CMLog::Plog("have the same user name, cur=%d", id);
            }
        }
        CMLog::Plog("CMyDb::GetUserId,id = %d", id);

        db.close();
    }
    catch (CppSQLite3Exception &e)
    {
        CMLog::Plog("CMyDb::GetUserId: %d : %s", e.errorCode(), e.errorMessage());
        db.close();
    }

    return id;
}

int CMyDb::CheckUser(char *name, char *pwd)
{
    int id = 0;
	CppSQLite3DB db;
	try
	{
		db.open(g_szDbFile);

		CppSQLite3Buffer sql;
        sql.format("SELECT id FROM user WHERE NAME='%s' AND PWD='%s';", name, pwd);
		CppSQLite3Query q = db.execQuery(sql);
        while (!q.eof())
        {
            id = q.getIntField(0);
            q.nextRow();
            if (!q.eof())
            {
                CMLog::Plog("exist same user name and pwd , cur id = %d", id);
            }
        }

        CMLog::Plog("CMyDb::CheckUser,id = %d", id);

        db.close();
    }
    catch (CppSQLite3Exception &e)
    {
        CMLog::Plog("CMyDb::CheckUser: %d : %s", e.errorCode(), e.errorMessage());
        db.close();
    }

    return id;
}

